<?php
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('article');
$ctable = Table('article_category');
$ltable = Table('lottery');

if(IS_POST)
{
    $id = intval($_REQUEST['id']);
    $data = $id ? $db->getrow('SELECT * FROM '.$table.' WHERE id = '.$id) : [];
    
    $ins = [];
    $ins['cid']		=   intval($_POST['cid']);
    $ins['title']	=   NoBadStr($_POST['title']);
    $ins['thumb']	=   NoBadStr($_POST['thumb']);
    $ins['keyword']	=   NoBadStr($_POST['keyword']);
    $ins['description']=NoBadStr($_POST['description']);
    $ins['content']	=   $_POST['content'];
    $ins['lotteryid']=   intval($_POST['lotteryid']);
	$ins['status'] 	= 	1;
	
    if($data['id'])
	{
        //修改
        $db->exec('UPDATE '.$table.' SET '.CreateUpdateSql($ins).' WHERE id = '.$data['id']);
    }
	else
	{
		$ins['dateline']=   TIMESTAMP;
		$db->add($ins);
	
		$db->exec('INSERT INTO '.$table.CreateInsertSql($ins));
    }
	
    ShowMsg('操作成功',U($m.'/'.$c));
}
	
$categorys = $db->getall('SELECT * FROM '.$ctable,'id');
$lotterys = $db->getall('SELECT * FROM '.$ltable,'id');

if($a == 'add')
{
    $id = intval($_GET['id']);
    $data = $id ? $db->getrow('SELECT * FROM '.$table.' WHERE id = '.$id) : [];

    $tpl->display($m.'/index_add');
}
else if($a == 'del')
{
    $id = intval($_GET['id']);
	$id && $db->exec('DELETE FROM '.$table.' WHERE id = '.$id);
    ShowMsg('删除成功',U($m.'/'.$c));
	
    // AjaxReturn(['error'=>'0','msg'=>'删除成功']);
}
else
{
    $data  = [];
    $where = ' FROM '.$table.' t WHERE 1 ';
	
	$cid = intval($_GET['cid']);
	$cid && $where .= ' AND cid = '.$cid;
	
	$title = NoBadStr($_GET['title']);
	$title && $where .= ' AND title LIKE \'%'.$title.'%\'';
    $tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
    if($tmp['p'])
	{
        $page = max(1,intval($_GET['page']));
        $size = 20;
        $limit    = ' LIMIT '.($page - 1) * $size.','.$size;

		$url = '?m='.$m.'&c='.$c.'&title='.$title.'&cid='.$cid;
        $orderby = ' ORDER BY t.id DESC ';
		
        $data = $db->getall('SELECT t.* '.$where.$orderby.$limit);
		
        $multi = Multi($url,$page,$tmp['p'],$size);
    }

    $tpl->display($m.'/index');
}